﻿using System;
using System.Data;
using System.Windows.Forms;
using ApplicationLibrary;
using ApplicationLibrary.CounterManager;
using ApplicationLibrary.DBManager;
using ApplicationLibrary.ExtendedControls;
using Vintner.Items.ModuleData;
using Vintner.Masters.ModuleData;
using Vintner.Sales.Forms;
using Vintner.Sales.ModuleData;

namespace Vintner.Sales.Library
{
    internal class SaleInvoicesCreator : SaleInvoices, IDocumentDataManager
    {
        public string DocumentNo { get { return FindControl<CounterControl>("fncSalesNo").Text; } }

        private RRItemCategory rrItemCategory = null;

        public SaleInvoicesCreator()
        {
            rrItemCategory = new RRItemCategory();
        }

        public bool Find(string code)
        {
            RadarCustomerParam param = new RadarCustomerParam(code);
            return FindRecord(param);
        }

        public void SetNumberAndData(int num, DateTime date)
        {
            CounterControl fncSaleNo = FindControl<CounterControl>("fncSalesNo");
            fncSaleNo.SuffixValue = date.Year.ToString().Substring(2);
            fncSaleNo.CounterNumericValue = num;
            fncSaleNo.IsAutomatic = false;

            CalendarTextBox ctb = FindControl<CalendarTextBox>("ctbDate");
            ctb.Today = date;
        }

        public void SetCustomerData(string cli, string dest)
        {
            RadarCounters fncCustomer = FindControl<RadarCounters>("fncCustomer");
            fncCustomer.IsAutomatic = false;
            fncCustomer.CounterNumericValue = int.Parse(cli);
            LoadDataCustomerInInvoice(false);
            FindControl<ComboBox>("cbbDestGoods").Text = dest;
        }

        public void SetAppereance(int colli, double litri, bool cartoni, bool cestelli, bool damigiane, bool fusti)
        {
            FindControl<NumericTextBox1>("ntbPieces").Int = colli;
            FindControl<NumericTextBox1>("ntbLitres").Double = litri;
            string app = "";
            if (cartoni)
                app = app.CommaConcat("CAR");
            if (cestelli)
                app = app.CommaConcat("CES");
            if (damigiane)
                app = app.CommaConcat("DAM");
            if (fusti)
                app = app.CommaConcat("FUS");
            FindControl<ExCheckedListBox>("clbAppearance").Value = app;
        }

        public void SetTransportData(DateTime dataTrasp, string time, string condpag, string mezzo)
        {
            FindControl<DateTimePicker>("dtpTransport").Value = new DateTime(dataTrasp.Year, dataTrasp.Month, dataTrasp.Day, int.Parse(time.Substring(0, 2)), int.Parse(time.Substring(4)), 0);

            FindControl<ComboBox>("cbbTransport").Text = mezzo;
            FindControl<ComboBox>("cbbPayment").Text = condpag;
        }

        public void ClearRows()
        {
            ExtendedDataGridView dgw = FindControl<ExtendedDataGridView>("dgwDetails");
            foreach (DataGridViewRow row in dgw.Rows)
                if (!row.IsNewRow)
                    dgw.Rows.Remove(row);
        }

        public void AddRow(string code, string desc, double quant, string iva, double litri, double prezzo, string lotto)
        {
            ExtendedDataGridView dgw = FindControl<ExtendedDataGridView>("dgwDetails");

            DataRowView drw = dgw.AddNewRow(VI_SaleInvoicesDetails.Line);
            DataRowViewValues.SetValue<string>(drw, VI_SaleInvoicesDetails.Item, code);
            DataRowViewValues.SetValue<string>(drw, VI_SaleInvoicesDetails.Description, desc);
            DataRowViewValues.SetValue<double>(drw, VI_SaleInvoicesDetails.Quantity, quant);
            DataRowViewValues.SetValue<string>(drw, VI_SaleInvoicesDetails.TaxCode, iva);
            DataRowViewValues.SetValue<double>(drw, VI_SaleInvoicesDetails.Litres, litri);
            DataRowViewValues.SetValue<double>(drw, VI_SaleInvoicesDetails.Price, prezzo);
            DataRowViewValues.SetValue<string>(drw, VI_SaleInvoicesDetails.Lot, lotto);

            //DataRowViewValues.SetValue<double>(drw, VI_SaleInvoicesDetails.Total, quant * prezzo);

            DataRowViewValues.SetValue<string>(drw, VI_SaleInvoicesDetails.Category, FindCategory(code, prezzo));
        }

        private string FindCategory(string code, double prezzo)
        {
            if (rrItemCategory.Find(code))
            {
                for (int t = 0; t < rrItemCategory.Count; t++)
                    if (rrItemCategory.GetValue<double>(VI_ItemsPrice.Price, t) == prezzo)
                        return rrItemCategory.GetValue<string>(VI_ItemsPrice.Category, t);
                return string.Empty;
            }
            else
                return string.Empty;
        }
    }
}